package com.powergisol.gis.support;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powergisol.gis.geom.GeoPoint;
import com.powergisol.gis.geom.GeoPolygon;
import com.powergisol.gis.dto.ModuleDTO;
import com.powergisol.gis.dto.ModulePoygonDTO;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ModuleJSONResolver implements ModuleResolver {


    public static void main(String[] args) {

        ModuleJSONResolver resolver = new ModuleJSONResolver();
        List<GeoPolygon> listPolygon = resolver.getListPolygon(new File("d:\\plane.txt"));

        GeoPoint point = new GeoPoint(113.227547, 23.1873188, 12.3389959);

        listPolygon.forEach(geoPolygon -> {

            Point p = point.toGeometry();
            Polygon polygon = geoPolygon.toGeometry();
            if(polygon.contains(p)){
                System.out.println(polygon);
            }

        });
        //testMethod();
    }

    static void testMethod() {

        Coordinate coordinate = new Coordinate(113.227577, 23.1873169, 12.3461647);
        Coordinate coordinate1 = new Coordinate(113.227562, 23.18734, 12.3526878);
        Coordinate coordinate2 = new Coordinate(113.227547, 23.1873188, 12.3389959);
        Coordinate coordinate3 = new Coordinate(113.227577, 23.1873169, 12.3461647);

        Coordinate[] coordinates = new Coordinate[]{coordinate, coordinate1, coordinate2, coordinate3};
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        Polygon polygon = geometryFactory.createPolygon(coordinates);
        Point point = geometryFactory.createPoint(coordinate);

        System.out.println(polygon.intersects(point));

    }




    @Override
    public List<ModulePoygonDTO> getListPolygon(String module) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        List<ModuleDTO> resultList = mapper.readValue(module, new TypeReference<List<ModuleDTO>>() {
        });
        List<ModulePoygonDTO> dtos = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {

            ModuleDTO moduleDTO = resultList.get(i);

            List<List<Integer>> conbinations = moduleDTO.getConbination();
            List<List<Double>> points = moduleDTO.getPoints();
            for (int j = 0; j < conbinations.size(); j++) {

                List<Integer> integers = conbinations.get(j);
                List<Coordinate> coordinates = new ArrayList<>();
                integers.forEach(num -> {
                    List<Double> doubles = points.get(num);
                    if (doubles.size() == 3) {
                        Coordinate coordinate = new Coordinate(doubles.get(0), doubles.get(1), doubles.get(2));
                        coordinates.add(coordinate);
                    } else {
                        throw new RuntimeException("points 格式出错:" + points);
                    }

                });

                Coordinate[] objects = coordinates.toArray(new Coordinate[coordinates.size()]);
                GeoPolygon polygon = new GeoPolygon(objects);
                ModulePoygonDTO drillDTO = new ModulePoygonDTO(moduleDTO.getLevel(), polygon);
                dtos.add(drillDTO);
            }

        }
        return dtos;
    }

    @Override
    public List<ModuleDTO> getListModuleDTO(String module) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        List<ModuleDTO> resultList = mapper.readValue(module, new TypeReference<List<ModuleDTO>>() {
        });

        return resultList;
    }


    public List<GeoPolygon> getListPolygon(File module) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            List<ModuleDTO> resultList = mapper.readValue(module, new TypeReference<List<ModuleDTO>>() {
            });
            List<GeoPolygon> polygons = new ArrayList<>();
            for (int i = 0; i < resultList.size(); i++) {

                ModuleDTO moduleDTO = resultList.get(i);

                List<List<Integer>> conbinations = moduleDTO.getConbination();
                List<List<Double>> points = moduleDTO.getPoints();
                for (int j = 0; j < conbinations.size(); j++) {

                    List<Integer> integers = conbinations.get(j);
                    List<Coordinate> coordinates = new ArrayList<>();
                    integers.forEach(num -> {
                        List<Double> doubles = points.get(num);
                        if (doubles.size() == 3) {
                            Coordinate coordinate = new Coordinate(doubles.get(0), doubles.get(1), doubles.get(2));
                            coordinates.add(coordinate);
                        } else {
                            throw new RuntimeException("points 格式出错:" + points);
                        }

                    });

                    Coordinate[] objects = coordinates.toArray(new Coordinate[coordinates.size()]);
                    GeoPolygon polygon = new GeoPolygon(objects);
                    polygons.add(polygon);
                }

            }
            return polygons;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
